package org.netEase.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.netEase.dao.zhanghuDao;
import org.netEase.dao.impl.zhanghuimpl;
import org.netEase.entity.zhanghu;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

@WebServlet(urlPatterns= {"/backstageManager/aadminadd.shtml" })
public class adminadd extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
		 SmartUpload smartUpload = new SmartUpload();
			smartUpload.initialize(this, request, response);
			try {
				smartUpload.upload();
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
			Request req = smartUpload.getRequest();
			String lei=req.getParameter("lei");
			String namesp=req.getParameter("namesp");
			String name=req.getParameter("name");
			String password=req.getParameter("password");
			String toimg=req.getParameter("toimg");
			String sex=req.getParameter("sex");
			String pione=req.getParameter("pione");
			String dataday=req.getParameter("dataday");
			String schoolname=req.getParameter("schoolname");
			Files files = smartUpload.getFiles();
			File file = files.getFile(0);
			zhanghu zh=new zhanghu();
			zh.setLei(Integer.parseInt(lei));
			zh.setNamesp(namesp);
			zh.setName(name);
			zh.setPassword(password);
			zh.setToimg(toimg);
			zh.setSex(sex);
			zh.setPione(pione);
			zh.setSchoolname(schoolname);
			  try {
				  zh.setDataday(sdf.parse(dataday));
				} catch (java.text.ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			  if(file.isMissing() == false){
					try {
						file.saveAs("./images/index/"+file.getFileName());
					} catch (SmartUploadException e) {
						e.printStackTrace();
					}
					
					zh.setToimg(file.getFileName());

				}
			  zhanghuDao zhanghudao=new zhanghuimpl();
			  int result=zhanghudao.add(zh);
				if(result > 0){
					response.sendRedirect(request.getContextPath() + "/backstageManager/adminsuer.shtml");
				}
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}
